package com.sanben.region.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sanben.common.constant.Constant;
import com.sanben.region.data.vo.RegionVo;
import com.sanben.region.entity.Region;
import com.sanben.region.mapper.RegionMapper;
import com.sanben.region.service.IRegionService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author cyh
 * @description: 城市表 服务实现类
 * @date: 2020-08-11
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {

    @Override
    public List<Region> getHomeHotList() {
        LambdaQueryWrapper<Region> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(Region::getIsHot, Constant.ONE);
        return this.list(lambdaQuery);
    }

    @Override
    public RegionVo getHomeList() {
        RegionVo regionVo = new RegionVo();
        // 查询热门城市
        List<Region> hotList = this.getHomeHotList();
        regionVo.setHotList(hotList);
        // 按首字母查排序所有城市
        LambdaQueryWrapper<Region> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.isNotNull(Region::getPinyin);
        lambdaQuery.orderByAsc(Region::getPinyin);
        List<Map<String, Object>> allCityList = this.listMaps(lambdaQuery);
        //获取全部字母
        Map<String, List<Object>> mapZm = new LinkedHashMap<>();
        String[] zm = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M",
                "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"};
        for (int i = 0; i < zm.length; i++) {
            mapZm.put(zm[i], new ArrayList<Object>());
        }
        //将对应的list放入相应的字母
        for (Map<String, Object> map : allCityList) {
            //暂时存放首字母
            String pinyin = String.valueOf(map.get("pinyin")).substring(0, 1).toUpperCase();
            //如果mapZm的key包含pinyin
            if (mapZm.containsKey(pinyin)) {
                mapZm.get(pinyin).add(map);
            }
        }
        regionVo.setAllCityList(mapZm);
        return regionVo;
    }

    @Override
    public Region detail(Integer code) {
        return this.getById(code);
    }

}
